﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KTDS_IPBS_WEB
{
    public partial class Content2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_start_Click(object sender, EventArgs e)
        {
            //클래스 선언 부
            C_Traceroute c_trace = new C_Traceroute();
            C_IPBS ipbs = new C_IPBS();
            C_DBConnect dbcon = new C_DBConnect();

            List<string> Lst_Src_Ip = new List<string>();
            List<string> Lst_Dst_Ip = new List<string>();
            List<string> Lst_Result_IP = new List<string>();
            InfoOfTraceIP st_Result = new InfoOfTraceIP();

            //소스 아이피 입력 받기
            //데스트 아이피 입력 받기
            String Src_Ip_Str = txb_Src_str.Text;
            String Dst_Ip_Str = txb_Dst_str.Text;

            if (Src_Ip_Str != "" || Dst_Ip_Str != "")
            {
                //트래이스 결과 받기(Src 입력)
                Lst_Src_Ip = c_trace.GetTraceRoute(Src_Ip_Str);

                //트래이스 결과 받기(Dst 입력)
                Lst_Dst_Ip = c_trace.GetTraceRoute(Dst_Ip_Str);


                //트래이스 결과 비교(List src, List dst)
                Lst_Result_IP = ipbs.test(Lst_Src_Ip, Lst_Dst_Ip);

                list_Result.Items.Clear();

                for (int i = 0; i < Lst_Result_IP.Count(); i++)
                {
                    list_Result.Items.Add(Lst_Result_IP[i].ToString());
                }


                List<string> lst_tablename = new List<string>();
                lst_tablename.Add("호스트ID");
                lst_tablename.Add("IP");
                lst_tablename.Add("호스트명");
                lst_tablename.Add("구간");
                lst_tablename.Add("장비종류");
                lst_tablename.Add("담당자이름");
                lst_tablename.Add("담당자부서");
                lst_tablename.Add("연락처");

                TableRow row_top = new TableRow();
                for (int i = 0; i < lst_tablename.Count(); i++)
                {
                    TableCell cell = new TableCell();
                    cell.Text = lst_tablename[i].ToString();
                    row_top.Cells.Add(cell);
                }
                table2.Rows.Add(row_top);

                List<string> lst_Result = new List<string>();
                for (int i = 0; i < Lst_Result_IP.Count(); i++)
                {
                    TableRow row = new TableRow();
                    st_Result = dbcon.SearchInformationOfTraceIP(Lst_Result_IP[i]);

                    lst_Result.Clear();
                    lst_Result.Add(st_Result.host_id);
                    lst_Result.Add(Lst_Result_IP[i]);
                    lst_Result.Add(st_Result.host_name);
                    lst_Result.Add(st_Result.loc);
                    lst_Result.Add(st_Result.type);
                    lst_Result.Add(st_Result.name);
                    lst_Result.Add(st_Result.dept);
                    lst_Result.Add(st_Result.phone);

                    if (Lst_Result_IP[i] == Src_Ip_Str)
                    {
                        lst_Result[2] = "출발지";
                    }

                    if (Lst_Result_IP[i] == Dst_Ip_Str)
                    {
                        lst_Result[2] = "목적지";
                    }

                    for (int y = 0; y < lst_Result.Count(); y++)
                    {
                        TableCell cell = new TableCell();
                        if (lst_Result[y] == null)
                        {
                            cell.Text = "-";
                        }
                        else
                        {
                            cell.Text = lst_Result[y].ToString();
                        }
                        row.Cells.Add(cell);
                    }

                    table2.Rows.Add(row);
                }
            }
            else
            {
                list_Result.Items.Clear();
                list_Result.Items.Add("Sourcㄷ IP와 Destination IP를 입력하세요");
            }
        }
    }
}